/**
 * @see https://umijs.org/zh-CN/plugins/plugin-access
 * */
export default function access(initialState: { currentUser?: API.CurrentUser | undefined }) {
  const { currentUser } = initialState || {};
  let module = currentUser?.module ?? [];
  let moduleData = currentUser?.moduleData ?? [];
  return {
    canAdmin: currentUser && currentUser.access === 'admin',
    canRoot: currentUser && currentUser.access === 'root',
    hasSc: currentUser && module.indexOf('SC') >= 0,
    hasTr: currentUser && module.indexOf('TR') >= 0,
    hasBs: currentUser && module.indexOf('BS') >= 0,
    isApprove: currentUser && currentUser.approve === 2,
    moduleData: (code: string) => {
      for (let i = 0; i < moduleData.length; i++) {
        let moduleDatum = moduleData[i];
        if (moduleDatum.code == code) {
          return moduleDatum;
        }
      }
      return {};
    },
    checkExpired: (code: string) => {
      // debugger;
      for (let i = 0; i < moduleData.length; i++) {
        let moduleDatum = moduleData[i];
        if (moduleDatum.code === code && moduleDatum.expired) {
          return true;
        }
      }
      return false;
    },
    canStudy: true,
  };
}
